package com.wuxiaofan.member.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxiaofan.member.entity.Member;
import com.wuxiaofan.member.entity.custom.MemberCustom;
import com.wuxiaofan.member.mapper.MemberMapper;
import java.io.IOException;
import java.util.Collections;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wuxiaofan.member.client.Idgenerator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Service
@Slf4j
public class MemberService {
    @Autowired
    private MemberMapper umsMemberMapper;
    @Resource
    private Idgenerator idgenerator;
    @Resource
    private SqlSessionTemplate sqlSessionTemplate;


    public Member findById(String id){
        return umsMemberMapper.selectById(id);
    }

    public MemberCustom findCountByMap(Map paraMap){
        return umsMemberMapper.findCountByMap(paraMap);
    }

    public MemberCustom findGroupByMap(Map paraMap){
        return umsMemberMapper.findGroupByMap(paraMap);
    }

    public List<MemberCustom> findByMap(Map paraMap){
        return umsMemberMapper.findByMap(paraMap);
    }

    public MemberCustom findByUsername(String username){
        Map paraMap = new HashMap();
        paraMap.put("username",username);
        List<MemberCustom> memberCustoms = umsMemberMapper.findByMap(paraMap);
        if (CollectionUtils.isEmpty(memberCustoms)){
            return null;
        }
        return memberCustoms.get(0);
    }

    public void queryByLikeUsername(String username) throws IOException {
        SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession();
        Cursor<MemberCustom> memberCustoms =
                sqlSession.getMapper(MemberMapper.class).queryByUsername(username);
        memberCustoms.forEach(memberCustom -> {
            log.info("memberCustom:{}",memberCustom);
        });
        memberCustoms.close();
    }

    @Transactional
    public void queryByLikeUsername2(String username) throws IOException {
        Cursor<MemberCustom> memberCustoms = umsMemberMapper.queryByUsername(username);
        try {
            memberCustoms.forEach(memberCustom -> {
                log.info("memberCustom:{}", memberCustom);
            });
        }catch (Exception e){
            log.error("会员游标查询失败");
        }
    }

    public Page<MemberCustom> findByMap(Map paraMap, Page page){
        return page.setRecords(umsMemberMapper.findByMap(paraMap,page));
    }


    public void updateMapById(Map paraMap){
        umsMemberMapper.updateMapById(paraMap);
    }
    public void delById(String id){
        umsMemberMapper.deleteById(id);
    }

    public Member save(Member umsMember){
        umsMember.setId(idgenerator.getId());
        umsMemberMapper.insert(umsMember);
        return umsMember;
    }

}
